package com.hoshiicloud.common.validate.annotations.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.hoshiicloud.common.validate.annotations.Mobile;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 手机号码验证接口实现类
 *
 * @author : yangcheng
 * @date : 2019/5/13 14:08
 * @mail : maiboyc@163.com
 **/
@Service
public class MobileValidatorImpl implements ConstraintValidator<Mobile, String> {

    private boolean state;

    @Override
    public void initialize(Mobile constraintAnnotation) {
        state = constraintAnnotation.state();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if(StringUtils.isBlank(value)){
            return false;
        }
        String regex = "^(13[0-9]|14[5|6|7|8|9]|15[0|1|2|3|5|6|7|8|9]|16[5|6]|17[0-8]|18[0-9]|19[1|8|9])\\d{8}$";
        if (value.matches(regex)) {
            return true;
        } else {
            return false;
        }
    }

}